package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MesBuyDetails;
import com.ruoyi.system.service.IMesBuyDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购订单明细Controller
 * 
 * @author ruoyi
 * @date 2024-06-17
 */
@RestController
@RequestMapping("/system/details")
public class MesBuyDetailsController extends BaseController
{
    @Autowired
    private IMesBuyDetailsService mesBuyDetailsService;

    /**
     * 查询采购订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:details:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesBuyDetails mesBuyDetails)
    {
        startPage();
        List<MesBuyDetails> list = mesBuyDetailsService.selectMesBuyDetailsList(mesBuyDetails);
        return getDataTable(list);
    }

    /**
     * 导出采购订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:details:export')")
    @Log(title = "采购订单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesBuyDetails mesBuyDetails)
    {
        List<MesBuyDetails> list = mesBuyDetailsService.selectMesBuyDetailsList(mesBuyDetails);
        ExcelUtil<MesBuyDetails> util = new ExcelUtil<MesBuyDetails>(MesBuyDetails.class);
        util.exportExcel(response, list, "采购订单明细数据");
    }

    /**
     * 获取采购订单明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:details:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(mesBuyDetailsService.selectMesBuyDetailsById(id));
    }

    /**
     * 新增采购订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:details:add')")
    @Log(title = "采购订单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesBuyDetails mesBuyDetails)
    {
        return toAjax(mesBuyDetailsService.insertMesBuyDetails(mesBuyDetails));
    }

    /**
     * 修改采购订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:details:edit')")
    @Log(title = "采购订单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesBuyDetails mesBuyDetails)
    {
        return toAjax(mesBuyDetailsService.updateMesBuyDetails(mesBuyDetails));
    }

    /**
     * 删除采购订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:details:remove')")
    @Log(title = "采购订单明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mesBuyDetailsService.deleteMesBuyDetailsByIds(ids));
    }

    /*****/
    //查看采购订单拥有哪些原料及其数量
    @GetMapping("/ord/matIds")
    public AjaxResult selectOrdMats(@RequestParam("buyOrdId") String buyOrdId) {
        List<String> ordMats = mesBuyDetailsService.selectOrdMats(buyOrdId);
        return AjaxResult.success(ordMats);
    }
}
